package aop.pointcut;

import lombok.Data;
import org.aspectj.weaver.tools.PointcutExpression;
import org.aspectj.weaver.tools.PointcutParser;
import org.aspectj.weaver.tools.ShadowMatch;

import java.lang.reflect.Method;

@Data
public class AspectJExpressionPointcut implements IPointcut
{
    private  static  PointcutParser pp = PointcutParser
        .getPointcutParserSupportingAllPrimitivesAndUsingContextClassloaderForResolution();

    private PointcutExpression pe;

    private String expression;

    public AspectJExpressionPointcut(String expression)
    {
        this.expression = expression;
        pe = pp.parsePointcutExpression(expression);
    }

    public boolean matchClass(Class targetClass)
    {
        return pe.couldMatchJoinPointsInType(targetClass);
    }

    public boolean matchMethod(Method method, Class targetClass)
    {
        ShadowMatch sm = pe.matchesAdviceExecution(method);
        return sm.alwaysMatches();
    }

}
